#include "sqlimagemodel.h"
#include "mainwindow.h"
#include "studydbmanager.h"

#include "dcmtk/dcmdata/dcuid.h"

#include <QSqlError>
#include <QFile>
#include <QDir>
#include <QDateTime>

SqlImageModel::SqlImageModel(QObject *parent, QSqlDatabase db) :
    QSqlTableModel(parent, db)
{
    setEditStrategy(QSqlTableModel::OnRowChange);
    setTable("ImageTable");
    setSort(ImageTime, Qt::DescendingOrder);
}

QVariant SqlImageModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (Qt::DisplayRole == role) {
        if (Qt::Horizontal == orientation) {
            switch (section) {
            case ImageNo:
                return tr("Image No.");
            case ImageTime:
                return tr("Image Time");
            case BodyPart:
                return tr("Body Part");
            case IsPrinted:
                return tr("Printed");
            case IsSent:
                return tr("Sent");
            case ImageDesc:
                return tr("Image Desc");
            case ImageFile:
                return tr("Image File");
            default:
                return QSqlTableModel::headerData(section, orientation, role);
            }
        }
    }
    return QSqlTableModel::headerData(section, orientation, role);
}

QStringList SqlImageModel::getAllImageFiles() const
{
    QStringList files;
    for (int i = 0; i < rowCount(); ++i) {
        QString file = data(index(i, ImageFile)).toString();
        files << file;
    }
    return files;
}

void SqlImageModel::onViewImages(const QModelIndexList &indexes)
{
    QStringList files;
    foreach (QModelIndex idx, indexes) {
        if (idx.column() == ImageFile)
            files << data(index(idx.row(), ImageFile)).toString();
    }
    emit viewImages(files);
}

void SqlImageModel::onSendImages(const QModelIndexList &indexes)
{
    QStringList files;
    foreach (QModelIndex idx, indexes) {
        if (idx.column() == ImageFile)
            files << data(index(idx.row(), ImageFile)).toString();
    }
    emit sendImages(files);
}

void SqlImageModel::onExportImages(const QModelIndexList &indexes)
{
    QStringList files;
    foreach (QModelIndex idx, indexes) {
        if (idx.column() == ImageFile)
            files << data(index(idx.row(), ImageFile)).toString();
    }
    emit exportImages(files);
}

void SqlImageModel::onRemoveImages(const QModelIndexList &indexes)
{
    foreach (QModelIndex idx, indexes) {
        if (idx.column() == ImageFile)
            StudyDbManager::removeImageFromDb(data(index(idx.row(), ImageUid)).toString());
    }
    select();
}

void SqlImageModel::onRemoveAllImages()
{
    QStringList studyUids;
    for (int i = 0; i < rowCount(); ++i) {
        QString uid = data(index(i, StudyUid)).toString();
        if (!studyUids.contains(uid)) studyUids << uid;
    }
    foreach (QString studyUid, studyUids) {
        StudyDbManager::removeAllImagesOfStudyFromDb(studyUid);
    }
    select();
}

void SqlImageModel::onStudySelected(const QStringList &studyUids)
{
    QString filter;
    if (studyUids.size()) {
        for (int i = 0; i < studyUids.size(); ++i) {
            filter.append(QString("StudyUid=\'%1\'").arg(studyUids.at(i)));
            if (i < studyUids.size()-1) filter.append(" OR ");
        }
    } else filter.append("StudyUid IS NULL");

    setFilter(filter);
}
